//
//  LanguageViewController.m
//  ChartsDemo
//
//  Created by wusiping on 2021/4/1.
//

#import "LanguageViewController.h"
#import "LanguageTableViewCell.h"
#import "ARTConfig.h"

@interface LanguageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *dataArray;

@end

@implementation LanguageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self dataConfig];
    [self UIConfig];
    J_Add_Notify(self, @selector(refreshLanguage), ART_Notify_LanguageChanged, nil);
    [self refreshLanguage];
}

- (void)refreshLanguage{
    self.title = FYLocalizedString(@"app__language", @"");
}

#pragma mark - dataConfig
- (void)dataConfig
{
    self.dataArray = @[
        @"简体中文",
        @"English",
        @"Français",
        @"Deutsch",
        @"Español"
    ];
}

#pragma mark - UIConfig
- (void)UIConfig{
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(-10, 0, 47, 27);
    [backBtn setImage:[UIImage imageNamed:@"i_return_black"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
     self.navigationItem.leftBarButtonItem = backItem;
    [self.view setBackgroundColor:J_ColorFromHEX(0xFBFBFB)];
 
    self.tableView = [[UITableView alloc] init];
    [self.tableView setBackgroundColor:[UIColor whiteColor]];
    self.tableView.layer.cornerRadius = 4;
//    self.tableView.layer.borderColor = [UIColor jk_colorWithHexString:@"#F1F1F1"].CGColor;
//    self.tableView.layer.borderWidth = 1.f;
    self.tableView.separatorColor = [UIColor colorWithString:@"#F1F1F1"];
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0 );
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top).offset(15);
        make.left.equalTo(self.view.mas_left).offset(15);
        make.right.equalTo(self.view.mas_right).offset(-15);
        make.height.mas_equalTo(240+60);
    }];
    [self.tableView registerClass:[LanguageTableViewCell class] forCellReuseIdentifier:@"LanguageTableViewCell"];

    
}

- (void)backClick
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - action
//设置语言
- (void)setLanguageWithlanguage:(NSInteger)language {
    [NSUserDefaults jk_setObject:@(language) forKey:SET_LANGUAGE];
}

#pragma mark - refresh
- (void)refresh
{
    [self.tableView reloadData];
}

#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    NSInteger count = 1;
    return count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSInteger count = self.dataArray.count;
    return count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LanguageTableViewCell *cell = (LanguageTableViewCell *)[self.tableView dequeueReusableCellWithIdentifier:@"LanguageTableViewCell"];
    if (!cell) {
        cell = [[LanguageTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                          reuseIdentifier:@"LanguageTableViewCell"];
    }
    NSString *title = self.dataArray[indexPath.row];
    NSString *language = [NSUserDefaults jk_stringForKey:SET_LANGUAGE];
    if (!language) {
        language = @"1";//默认英文
    }
    BOOL choosed = (indexPath.row == [language integerValue]) ? YES : NO;
    [cell configCellWithTitle:title isChoose:choosed];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [NSUserDefaults jk_setObject:[NSString stringWithFormat:@"%ld",(long)indexPath.row] forKey:SET_LANGUAGE];
    [self refresh];

    [[NSNotificationCenter defaultCenter] postNotificationName:ART_Notify_LanguageChanged object:nil];

}

- (void)dealloc{
    J_Remove_Notify(self);
}

@end
